<!--
 * @Description: 地址编辑
 * @Author: charles
 * @Date: 2021-10-25 13:22:44
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-10-26 19:59:10
-->
<template>
  <!-- <b-fullpage title="地址编辑"> -->
    <div>
      <!-- 返回 -->
      <van-nav-bar  
      @click-left="$router.back()"
      left-text="返回" 
      title="地址编辑"></van-nav-bar>
      <!-- 地址编辑组件 -->
      <van-address-edit
        :address-info="address"
        :area-list="areaList"
        show-delete
        show-set-default
        @save="onSave"
        @delete="onDelete"
      />
  </div>
  <!-- </b-fullpage> -->
</template>
<script>
import {ref,onMounted,computed} from 'vue'
import { areaList } from '@vant/area-data';
import {useStore} from 'vuex'
import {useRouter,useRoute} from 'vue-router'
import {get, post} from '../../../utils/request'
export default {
  setup(){
    const searchResult = ref([]);
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    const address = ref({})
    
    let user_info = computed(()=> store.state.user.user_info)

    const onSave = async (content) => {
      let data = {
        id:content.id,
        realname:content.name,
        telephone:content.tel,
        province:content.province,
        city:content.city,
        area:content.county,
        address:content.addressDetail,
        isDefault:content.isDefault?1:0,
        areaCode:content.areaCode,
        userId:user_info.value.id
      }
      let url = "/app/address/saveOrUpdate"
      let resp = await post(url,data);
      router.push('/address')
    };
    const onDelete = async ({id}) => {
      let url ="/app/address/deleteById"
      let resp = await get(url,{id})
      router.push('/address')
    };

    onMounted(()=>{
      let params = route.params;
      if(params && params.address){
        let pca = params.address.split(' ');
        address.value = {
          ...params,
          province:pca[0],
          city:pca[1],
          county:pca[2],
          addressDetail:pca[3]
        };
      }

     
    })

    return {
      onSave,
      onDelete,
      areaList,
      searchResult,
      address
    };
  }
}
</script>
